rm(list=ls())
library(foreign)
library(dplyr)
library(ggplot2)
library(spatialEco)
library(gtable)
library(grid)

mydata <- read.dta("IHDS_final.dta")

Conf_pol <- subset(mydata,  sfe_prev_monsoon2!="" & sfe_CI1_right!="")
Conf_state <- subset(mydata,  sfe_prev_monsoon2!="" & sfe_CI4_right!="")
Conf_panc <- subset(mydata,  sfe_prev_monsoon2!="" & sfe_CI6_right!="")

newdata <- subset(mydata, year==2011 | year==2012)
card_Conf_pol <- subset(newdata, IN16_dum==1 & CI1_right!="")
nocard_Conf_pol <- subset(newdata, IN16_dum==0 & CI1_right!="")
card_Conf_state <- subset(newdata, IN16_dum==1 & CI4_right!="")
nocard_Conf_state <- subset(newdata, IN16_dum==0 & CI4_right!="")
card_Conf_panc <- subset(newdata, IN16_dum==1 & CI6_right!="")
nocard_Conf_panc <- subset(newdata, IN16_dum==0 & CI6_right!="")


##################################################################
# Main - Fig 6 (a)
##################################################################

lo.Conf_pol.f <- loess.boot(Conf_pol$sfe_prev_monsoon2,Conf_pol$sfe_CI1_right, nreps=100, confidence = 0.95,span=0.1)
lo.Conf_pol<-data.frame(lo.Conf_pol.f$fit)

bs_Conf_pol<-ggplot(aes(x = lo.Conf_pol$x, y = lo.Conf_pol$y.fit), data = lo.Conf_pol) +  
  geom_line() + 
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.3,0,0.3,1) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.Conf_pol, aes(ymin=lo.Conf_pol$low.lim,ymax=lo.Conf_pol$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Politicians, Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="LOWESS Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ###panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

bs_Conf_pol

gg_Conf_pol<-ggplot(data=Conf_pol, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        ##panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 6 (b)
##################################################################


fit <- lm(sfe_CI1_right ~ sfe_prev_monsoon2 + I(sfe_prev_monsoon2^2), data = Conf_pol)
prd <- data.frame(sfe_prev_monsoon2 = seq(from = range(Conf_pol$sfe_prev_monsoon2)[1], to = range(Conf_pol$sfe_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Conf_pol<-ggplot(prd, aes(x = sfe_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.5,0,0.5,1) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Politicians, Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="Quadratic Prediction Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

quad_Conf_pol

gg_Conf_pol<-ggplot(data=Conf_pol, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
##g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it
grid.draw(g)




##################################################################
# Main - Fig 6 (c)
##################################################################

lo.Conf_state.f <- loess.boot(Conf_state$sfe_prev_monsoon2,Conf_state$sfe_CI4_right, nreps=100, confidence = 0.95,span=0.1)
lo.Conf_state<-data.frame(lo.Conf_state.f$fit)

bs_Conf_state<-ggplot(aes(x = lo.Conf_state$x, y = lo.Conf_state$y.fit), data = lo.Conf_state) +  
  geom_line() + 
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.3,0,0.3,1) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.Conf_state, aes(ymin=lo.Conf_state$low.lim,ymax=lo.Conf_state$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (State Govt., Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="LOWESS Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ###panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Conf_state<-ggplot(data=Conf_state, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        ##panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 6 (d)
##################################################################


fit <- lm(sfe_CI4_right ~ sfe_prev_monsoon2 + I(sfe_prev_monsoon2^2), data = Conf_state)
prd <- data.frame(sfe_prev_monsoon2 = seq(from = range(Conf_state$sfe_prev_monsoon2)[1], to = range(Conf_state$sfe_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Conf_state<-ggplot(prd, aes(x = sfe_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.5,0,0.5,1) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (State Govt., Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="Quadratic Prediction Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Conf_state<-ggplot(data=Conf_state, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
##g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 6 (e)
##################################################################


lo.Conf_panc.f <- loess.boot(Conf_panc$sfe_prev_monsoon2,Conf_panc$sfe_CI6_right, nreps=100, confidence = 0.95,span=0.1)
lo.Conf_panc<-data.frame(lo.Conf_panc.f$fit)

bs_Conf_panc<-ggplot(aes(x = lo.Conf_panc$x, y = lo.Conf_panc$y.fit), data = lo.Conf_panc) +  
  geom_line() + 
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.3,0,0.3,1) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.Conf_panc, aes(ymin=lo.Conf_panc$low.lim,ymax=lo.Conf_panc$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Panchayats, Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="LOWESS Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ###panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Conf_panc<-ggplot(data=Conf_panc, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        ##panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 6 (f)
##################################################################


fit <- lm(sfe_CI6_right ~ sfe_prev_monsoon2 + I(sfe_prev_monsoon2^2), data = Conf_panc)
prd <- data.frame(sfe_prev_monsoon2 = seq(from = range(Conf_panc$sfe_prev_monsoon2)[1], to = range(Conf_panc$sfe_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Conf_panc<-ggplot(prd, aes(x = sfe_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(-0.3, 0.3),breaks = c(-0.3,0,0.3) ) + scale_x_continuous(limits = c(-1, 1),breaks = c(-1,-0.5,0,0.5,1) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Panchayats, Difference from Household Mean)", x="Prev. Monsoon (Difference from Household Mean)", title="Quadratic Prediction Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Conf_panc<-ggplot(data=Conf_panc, aes(sfe_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=3.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
##g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (a)
##################################################################


fit <- lm(CI1_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = card_Conf_pol)
prd <- data.frame(prev_monsoon2 = seq(from = range(card_Conf_pol$prev_monsoon2)[1], to = range(card_Conf_pol$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_card_Conf_pol<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Politicians)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_card_Conf_pol<-ggplot(data=card_Conf_pol, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_card_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (b)
##################################################################


fit <- lm(CI1_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = nocard_Conf_pol)
prd <- data.frame(prev_monsoon2 = seq(from = range(nocard_Conf_pol$prev_monsoon2)[1], to = range(nocard_Conf_pol$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_nocard_Conf_pol<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Politicians)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_pol<-ggplot(data=nocard_Conf_pol, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_nocard_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (c)
##################################################################


fit <- lm(CI4_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = card_Conf_state)
prd <- data.frame(prev_monsoon2 = seq(from = range(card_Conf_state$prev_monsoon2)[1], to = range(card_Conf_state$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_card_Conf_state<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (State Govt.)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_card_Conf_state<-ggplot(data=card_Conf_state, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_card_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (d)
##################################################################


fit <- lm(CI4_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = nocard_Conf_state)
prd <- data.frame(prev_monsoon2 = seq(from = range(nocard_Conf_state$prev_monsoon2)[1], to = range(nocard_Conf_state$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_nocard_Conf_state<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (State Govt.)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_state<-ggplot(data=nocard_Conf_state, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_nocard_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (e)
##################################################################


fit <- lm(CI6_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = card_Conf_panc)
prd <- data.frame(prev_monsoon2 = seq(from = range(card_Conf_panc$prev_monsoon2)[1], to = range(card_Conf_panc$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_card_Conf_panc<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Panchayats)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_card_Conf_panc<-ggplot(data=card_Conf_panc, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_card_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


##################################################################
# Main - Fig 7 (f)
##################################################################


fit <- lm(CI6_right ~ prev_monsoon2 + I(prev_monsoon2^2), data = nocard_Conf_panc)
prd <- data.frame(prev_monsoon2 = seq(from = range(nocard_Conf_panc$prev_monsoon2)[1], to = range(nocard_Conf_panc$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_nocard_Conf_panc<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Confidence (Panchayats)", x="Prev. Monsoon", title="Quadratic Prediction Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_panc<-ggplot(data=nocard_Conf_panc, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_nocard_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
#g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file




##################################################################
# APPENDIX SECTION A11 
##################################################################

participation <- subset(mydata,  prev_monsoon2!="" & participation!="")

############
# Fig A16 (a)
############


lo.participation.f <- loess.boot(participation$prev_monsoon2,participation$participation, nreps=1000, confidence = 0.95,span=0.1)
lo.participation<-data.frame(lo.participation.f$fit)

bs_participation<-ggplot(aes(x = lo.participation$x, y = lo.participation$y.fit), data = lo.participation) +  
  geom_line() + 
  coord_fixed(ratio=6) +
  scale_y_continuous(limits = c(0, 0.5),breaks = c(0, 0.1,0.2,0.3,0.4,0.5) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.participation, aes(ymin=lo.participation$low.lim,ymax=lo.participation$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Participation", x="Prev. Monsoon", title="LOWESS Plot: Participation") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_participation<-ggplot(data=participation, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        ##panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_participation))
g2 <- ggplot_gtable(ggplot_build(gg_participation))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


############
# Fig A16 (b)
############

fit <- lm(participation ~ prev_monsoon2 + I(prev_monsoon2^2), data = participation)
prd <- data.frame(prev_monsoon2 = seq(from = range(participation$prev_monsoon2)[1], to = range(participation$prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_participation<-ggplot(prd, aes(x = prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=6) +
  scale_y_continuous(limits = c(0, 0.5),breaks = c(0, 0.1,0.2,0.3,0.4,0.5) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5) ) +    
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Participation", x="Prev. Monsoon", title="Quadratic Prediction Plot: Participation") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_participation<-ggplot(data=participation, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        ##panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_participation))
g2 <- ggplot_gtable(ggplot_build(gg_participation))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file



##################################################################
# APPENDIX SECTION A12
##################################################################

participation <- subset(mydata,  prev_monsoon2!="" & participation!="")

############
# Fig A17 (a)
############


lo.card_Conf_pol.f <- loess.boot(card_Conf_pol$prev_monsoon2,card_Conf_pol$CI1_right, nreps=100, confidence = 0.95,span=0.1)
lo.card_Conf_pol<-data.frame(lo.card_Conf_pol.f$fit)

bs_card_Conf_pol<-ggplot(aes(x = lo.card_Conf_pol$x, y = lo.card_Conf_pol$y.fit), data = lo.card_Conf_pol) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.card_Conf_pol, aes(ymin=lo.card_Conf_pol$low.lim,ymax=lo.card_Conf_pol$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Politicians)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

bs_card_Conf_pol

gg_card_Conf_pol<-ggplot(data=card_Conf_pol, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_card_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


############
# Fig A17 (b)
############


lo.nocard_Conf_pol.f <- loess.boot(nocard_Conf_pol$prev_monsoon2,nocard_Conf_pol$CI1_right, nreps=100, confidence = 0.95,span=0.1)
lo.nocard_Conf_pol<-data.frame(lo.nocard_Conf_pol.f$fit)

bs_nocard_Conf_pol<-ggplot(aes(x = lo.nocard_Conf_pol$x, y = lo.nocard_Conf_pol$y.fit), data = lo.nocard_Conf_pol) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.nocard_Conf_pol, aes(ymin=lo.nocard_Conf_pol$low.lim,ymax=lo.nocard_Conf_pol$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Politicians)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (Politicians)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_pol<-ggplot(data=nocard_Conf_pol, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_nocard_Conf_pol))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_pol))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file


############
# Fig A17 (c)
############


lo.card_Conf_state.f <- loess.boot(card_Conf_state$prev_monsoon2,card_Conf_state$CI4_right, nreps=100, confidence = 0.95,span=0.1)
lo.card_Conf_state<-data.frame(lo.card_Conf_state.f$fit)

bs_card_Conf_state<-ggplot(aes(x = lo.card_Conf_state$x, y = lo.card_Conf_state$y.fit), data = lo.card_Conf_state) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.card_Conf_state, aes(ymin=lo.card_Conf_state$low.lim,ymax=lo.card_Conf_state$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (State Govt.)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_card_Conf_state<-ggplot(data=card_Conf_state, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_card_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it

grid.draw(g) # Write the grid.arrange in the file


############
# Fig A17 (d)
############


lo.nocard_Conf_state.f <- loess.boot(nocard_Conf_state$prev_monsoon2,nocard_Conf_state$CI4_right, nreps=100, confidence = 0.95,span=0.1)
lo.nocard_Conf_state<-data.frame(lo.nocard_Conf_state.f$fit)

bs_nocard_Conf_state<-ggplot(aes(x = lo.nocard_Conf_state$x, y = lo.nocard_Conf_state$y.fit), data = lo.nocard_Conf_state) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.nocard_Conf_state, aes(ymin=lo.nocard_Conf_state$low.lim,ymax=lo.nocard_Conf_state$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (State Govt.)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (State Govt.)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_state<-ggplot(data=nocard_Conf_state, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_nocard_Conf_state))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_state))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it

grid.draw(g) # Write the grid.arrange in the file


############
# Fig A17 (e)
############


lo.card_Conf_panc.f <- loess.boot(card_Conf_panc$prev_monsoon2,card_Conf_panc$CI6_right, nreps=100, confidence = 0.95,span=0.1)
lo.card_Conf_panc<-data.frame(lo.card_Conf_panc.f$fit)

bs_card_Conf_panc<-ggplot(aes(x = lo.card_Conf_panc$x, y = lo.card_Conf_panc$y.fit), data = lo.card_Conf_panc) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.card_Conf_panc, aes(ymin=lo.card_Conf_panc$low.lim,ymax=lo.card_Conf_panc$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Panchayats)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_card_Conf_panc<-ggplot(data=card_Conf_panc, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_card_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_card_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it

grid.draw(g) # Write the grid.arrange in the file


############
# Fig A17 (f)
############

lo.nocard_Conf_panc.f <- loess.boot(nocard_Conf_panc$prev_monsoon2,nocard_Conf_panc$CI6_right, nreps=100, confidence = 0.95,span=0.1)
lo.nocard_Conf_panc<-data.frame(lo.nocard_Conf_panc.f$fit)

bs_nocard_Conf_panc<-ggplot(aes(x = lo.nocard_Conf_panc$x, y = lo.nocard_Conf_panc$y.fit), data = lo.nocard_Conf_panc) +  
  geom_line() + 
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(1, 3),breaks = c(1,2,3) ) + scale_x_continuous(limits = c(-2, 1.5),breaks = c(-2,-1.5,-1,-0.3,0,0.3,1,1.5) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=lo.nocard_Conf_panc, aes(ymin=lo.nocard_Conf_panc$low.lim,ymax=lo.nocard_Conf_panc$up.lim), fill="grey50", alpha="0.3") +
  labs(y = "Confidence (Panchayats)", x="Prev. Monsoon", title="LOWESS Plot: Confidence (Panchayats)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        ##panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_nocard_Conf_panc<-ggplot(data=nocard_Conf_panc, aes(prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=1.5) +
  scale_y_continuous(limits = c(0, 0.3), breaks = c(0,0.1,0.2,0.3)) + 
  theme(axis.title.y=element_text(vjust=-0.3),
        axis.text.y = element_text(vjust=0),
        panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        #panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_nocard_Conf_panc))
g2 <- ggplot_gtable(ggplot_build(gg_nocard_Conf_panc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g <- gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths) , pp$b)
# draw it


grid.draw(g) # Write the grid.arrange in the file

